$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'sys/rebirth/user/company/list',
        datatype: "json",
        colModel: [			
			{ label: '用户ID', name: 'userId', index: 'userId', width: 50, key: true },
			// { label: '', name: 'userId', index: 'user_id', width: 60 },
			{ label: '手机号', name: 'mobile', index: 'mobile', width: 60 },
			{ label: '姓名', name: 'realName', index: 'real_name', width: 60 },
			{ label: '性别', name: 'sex', index: 'sex', width: 50 , formatter: function(value, options, row){
					if(value === 1){
						return  '男';
					}else if(value === 2){
						return  '女';
					}else{
						return "未知";
					}
				}},
			{ label: '推荐码', name: 'inviteCode', index: 'invite_code', width: 120 ,formatter: function (value, options, row) {
					var title = "推荐码";
					return "<img  style='width:30px;height: 30px;cursor:pointer;'  src='data:image/png;base64,"+value+"'   onclick='showImg(\""+value+"\",\""+title+"\")'/>";
				}
			},
			{ label: '支付宝收款码', name: 'aliQrCode', index: 'ali_qr_code', width: 120 ,formatter: function (value, options, row) {
					var title = "支付宝收款码";
					return "<img  style='width:30px;height: 30px;cursor:pointer;'  src='"+value+"'   onclick='showImg2(\""+value+"\",\""+title+"\")'/>";
				}},
			{ label: '微信收款码', name: 'wxQrCode', index: 'wx_qr_code', width: 120  ,formatter: function (value, options, row) {
					var title = "微信收款码";
					return "<img  style='width:30px;height: 30px;cursor:pointer;'  src='"+value+"'  onclick='showImg2(\""+value+"\",\""+title+"\")'/>";
				}},
			{ label: '开户名', name: 'bankRealName', index: 'bank_real_name', width: 120 },
			{ label: '开户行', name: 'bankName', index: 'bank_name', width: 120 },
			{ label: '开户行地址', name: 'bankAddress', index: 'bank_address', width: 120 },
			{ label: '银行卡号', name: 'bankAccount', index: 'bank_account', width: 120 },
			{ label: '注册时间', name: 'createTime', index: 'create_time', width: 160 }
        ],
		viewrecords: true,
		height: 385,
		rowNum: 10,
		rowList: [10, 30, 50],
		rownumbers: true,
		rownumWidth: 25,
		autowidth: true,
		multiselect: true,
		footerrow: true,//分页上添加一行，用于显示统计信息
		pager: "#jqGridPager",
		jsonReader: {
			root: "page.list",
			page: "page.currPage",
			total: "page.totalPage",
			records: "page.totalCount"
		},
		prmNames: {
			page: "page",
			rows: "limit",
			order: "order"
		}
    });
});

var vm = new Vue({
	el:'#rrapp',
	data:{
		showList: true,
		title: null,
		user: {}
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.user = {};
		},
		update: function (event) {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";
			vm.user = {};
            vm.getInfo(id)
		},
		initCompanyAccount: function (event) {
			var lock = false;
			layer.confirm('确定要生成公司账户？', {
				btn: ['确定','取消'] //按钮
			}, function(){
				if(!lock) {
					lock = true;
					$.ajax({
						type: "POST",
						url: baseURL + "sys/rebirth/user/initCompanyAccount",
						contentType: "application/json",
						success: function(r){
							if(r.code == 0){
								layer.msg("操作成功", {icon: 1});
								$("#jqGrid").trigger("reloadGrid");
							}else{
								layer.alert(r.msg);
							}
						}
					});
				}
			}, function(){
			});
		},
		fileUpload:function (thisFile,qrCodeId,imageUrl) {
			var formData = new FormData();
			formData.append('file', $("#"+thisFile)[0].files[0]);
			$.ajax({
				url:baseURL + "sys/slideshow/fileUpload/",
				type: 'POST',
				cache: false,
				data: formData,
				processData: false,
				contentType: false
			}).done(function(res) {
				debugger;
				if(qrCodeId == 'aliQrCodeUrl'){
					vm.user.aliQrCode = res.path;
				}else{
					vm.user.wxQrCode = res.path;
				}
				if(res.status=="true"){
					// $("#"+qrCodeId).removeAttr("src");
					$("#"+imageUrl).attr("src",res.path);
				}
			}).fail(function(res) {});
		},
		getRecommendQrCode: function (event) {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
		},
		saveOrUpdate: function (event) {
			var myreg = /^[1][3,4,5,6,7,8,9][0-9]{9}$/;
			var mobile = vm.user.mobile;
			if (mobile.length == 11 && myreg.test(mobile)) {
				$('#btnSaveOrUpdate').button('loading').delay(1000).queue(function() {
					$.ajax({
						type: "POST",
						url: baseURL + "sys/rebirth/user/initCompanyAccount",
						contentType: "application/json",
						data: JSON.stringify(vm.user),
						success: function(r){
							if(r.code === 0){
								layer.msg("操作成功", {icon: 1});
								vm.reload();
								$('#btnSaveOrUpdate').button('reset');
								$('#btnSaveOrUpdate').dequeue();
							}else{
								layer.alert(r.msg);
								$('#btnSaveOrUpdate').button('reset');
								$('#btnSaveOrUpdate').dequeue();
							}
						}
					});
				});
			}else{
				alert("请输入正确的手机号");
			}

		},
		del: function (event) {
			var id = getSelectedRows();
			if(id == null){
				return ;
			}
			var lock = false;
            layer.confirm('确定要删除选中的记录？', {
                btn: ['确定','取消'] //按钮
            }, function(){
               if(!lock) {
                    lock = true;
		            $.ajax({
                        type: "POST",
                        url: baseURL + "sys/rebirth/user/delete?id="+id,
                        contentType: "application/json",
                        success: function(r){
                            if(r.code == 0){
                                layer.msg("操作成功", {icon: 1});
                                $("#jqGrid").trigger("reloadGrid");
                            }else{
                                layer.alert(r.msg);
                            }
                        }
				    });
			    }
             }, function(){
             });
		},
		getInfo: function(id){
			$.get(baseURL + "sys/rebirth/user/info/"+id, function(r){
                vm.user = r.user;
				$("#wxQrCode").attr("src",vm.user.wxQrCode);
				$("#aliQrCode").attr("src",vm.user.aliQrCode);
            });
		},
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{ 
                page:1
            }).trigger("reloadGrid");
		}
	}
});

function showImg(val,title){
	$("#showImg").attr("src","data:image/png;base64," + val);
	layer.open({
		type: 1,
		skin: 'layui-layer-molv',
		title: title,
		area: ['260px', '350px'],
		shade: 0,
		shadeClose: false,
		content: jQuery("#Img"),
		btn: ['确定'],
		btn1: function (index) {
			layer.close(index);
		}
	});
}
function showImg2(val,title){
	$("#showImg").attr("src",val);
	layer.open({
		type: 1,
		skin: 'layui-layer-molv',
		title: title,
		area: ['650px', '500px'],
		shade: 0,
		shadeClose: false,
		content: jQuery("#Img"),
		btn: ['确定'],
		btn1: function (index) {
			layer.close(index);
		}
	});
}